//
//  LZGrabCouponPopScrollView.m
//  laziz_Consumer
//
//  Created by xbnzd on 17/4/18.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZGrabCouponPopScrollView.h"
#import "LZGrabCouponAndFightGroupsDetialModel.h"

#import "LZDateFormatterManager.h"
#import "Masonry.h"

static CGFloat selfWidth = 275.0;


@interface LZGrabCouponPopScrollView ()
@property (nonatomic, strong) UIButton *getCouponBtn;

@end

@implementation LZGrabCouponPopScrollView

#pragma mark - event
- (void)setBtnsUserInteractionEnabled:(BOOL)enabled
{
    self.getCouponBtn.userInteractionEnabled = enabled;
}

- (void)getCouponBtnAction
{
    if (self.couponTitleViewForPopDelegate && [self.couponTitleViewForPopDelegate respondsToSelector:@selector(grabCouponsAction)]) {
        [self.couponTitleViewForPopDelegate grabCouponsAction];
    }
}

#pragma mark - private method

- (void)setDetailModel:(LZGrabCouponAndFightGroupsDetialModel *)detailModel
{
    _detailModel = detailModel;
    
    [self configureUI];
}

- (void)setLabelContentText:(NSString *)text Label:(UILabel *)glabel
{
    if (text.length <= 0) {
        return;
    }
    extern CGFloat textLineSpace;
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    
    [style setLineSpacing:textLineSpace];
    [style setLineBreakMode:NSLineBreakByWordWrapping];
    [style setAlignment:NSTextAlignmentNatural];
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attStr.length)];
    glabel.attributedText = attStr;
}


#pragma mark - UI
- (void)configureUI
{
    GET_SCREEN_SCALE(scale);
    CGFloat textScale = scale;
    
    self.backgroundColor = [UIColor clearColor];
    
    if (self.subviews.count > 0) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColorFromRGB(0xffffff, 1);;
    [self addSubview:bgView];
    
    //底图
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0);
        make.width.mas_equalTo(selfWidth*scale);
    }];
    
    //优惠券名称 抢券按钮 剩余数量 横线
    UILabel *couponNameLabel = [self creatLabelWithTextColor:UIColorFromRGB(0xff7a21, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    couponNameLabel.text = self.detailModel.couponsName;
    [bgView addSubview:couponNameLabel];

    self.getCouponBtn = [self creatButtonWithTitle:LZLocalizedString(@"get_coupon_big", nil)];
    [self.getCouponBtn addTarget:self action:@selector(getCouponBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.getCouponBtn];
    
    UILabel *couponCountLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x999999, 1) Font:[UIFont systemFontOfSize:15*textScale] NumberLines:1];
    couponCountLabel.text = [NSString stringWithFormat:@"%ld %@",self.detailModel.couponsResidueCount,LZLocalizedString(@"group_left", nil)];
    [bgView addSubview:couponCountLabel];
    
    UIView *lineOne = [self creatLine];
    [bgView addSubview:lineOne];
    [self addMasonryWithCouponNamelLabel:couponNameLabel couponCountLabel:couponCountLabel line:lineOne getCouponBtn:self.getCouponBtn];
    
    //******************优惠券规则提醒 使用条件
    UIView *lineTwo;
    if (self.detailModel.couponsRemindRule.length != 0 || self.detailModel.couponsUseConditions.length != 0) {
        UILabel *titleLabelTwo = [self creatLabelWithTextColor:UIColorFromRGB(0x666666, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
        titleLabelTwo.text = LZLocalizedString(@"group_terms_of_use", nil);
        [bgView addSubview:titleLabelTwo];
        //规则提醒
        UILabel *rulesLabel;
        if (self.detailModel.couponsRemindRule.length != 0) {
            rulesLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
            rulesLabel.preferredMaxLayoutWidth = selfWidth*scale - 20;
            [self setLabelContentText:self.detailModel.couponsRemindRule Label:rulesLabel];
            
            [bgView addSubview:rulesLabel];
        }
        //使用条件
        UILabel *conditionsLabel;
        if (self.detailModel.couponsUseConditions.length != 0) {
            conditionsLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
            conditionsLabel.preferredMaxLayoutWidth = selfWidth*scale - 20;
            [self setLabelContentText:self.detailModel.couponsUseConditions Label:conditionsLabel];
            
            [bgView addSubview:conditionsLabel];
        }
        
        lineTwo = [self creatLine];
        [bgView addSubview:lineTwo];
        
        [self addMasonryWithLastView:lineOne rulesTitleLabel:titleLabelTwo conditionsLabel:conditionsLabel rluesLabel:rulesLabel line:lineTwo];
    }
    
    //*********** 优惠券 每天开始,结束时间, 优惠除外时间 每天有效时间 优惠券预约提醒
    UILabel *timeTitleLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x666666, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    timeTitleLabel.text = LZLocalizedString(@"group_meal_time", nil);
    [bgView addSubview:timeTitleLabel];
    //每天开始结束时间
    UILabel *everyDayBeginAndEndTimeNameLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    everyDayBeginAndEndTimeNameLabel.text = LZLocalizedString(@"mine_coupons_validity_period", nil);
    [bgView addSubview:everyDayBeginAndEndTimeNameLabel];
    
    UILabel *everyDayBeginAndEndTimeLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
    everyDayBeginAndEndTimeLabel.preferredMaxLayoutWidth = selfWidth*scale - 20;
    [self setLabelContentText:[NSString stringWithFormat:@"%@ - %@",[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:self.detailModel.couponsStartTime],[[LZDateFormatterManager shareInstance] stringFromDoubleTimestampHavePointyyyyMMdd:self.detailModel.couponsEndTime]] Label:everyDayBeginAndEndTimeLabel];
    [bgView addSubview:everyDayBeginAndEndTimeLabel];
    
    //优惠除外时间
    UILabel *exceptTimeNameLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    exceptTimeNameLabel.text = LZLocalizedString(@"mine_coupons_valid_except", nil);
    [bgView addSubview:exceptTimeNameLabel];
    
    UILabel *exceptTimeLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
    exceptTimeLabel.preferredMaxLayoutWidth = selfWidth*scale - 30 - [exceptTimeNameLabel intrinsicContentSize].width;
    [self setLabelContentText:self.detailModel.couponsExceptTime Label:exceptTimeLabel];
    [bgView addSubview:exceptTimeLabel];
    
    //每天有效时间
    UILabel *everyDayValidNameLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    everyDayValidNameLabel.text = LZLocalizedString(@"mine_coupons_everyday_valid", nil);
    [bgView addSubview:everyDayValidNameLabel];
    
    UILabel *everyDayValidLabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
    everyDayValidLabel.preferredMaxLayoutWidth = selfWidth*scale - 30 - [everyDayValidNameLabel intrinsicContentSize].width;
    [self setLabelContentText:[self.detailModel getCouponEverydayValid] Label:everyDayValidLabel];
    [bgView addSubview:everyDayValidLabel];
    
    //预约提醒
    UILabel *otherContentLable;
    if (self.detailModel.couponsRemindAppoint.length != 0) {
        otherContentLable = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
        otherContentLable.preferredMaxLayoutWidth = selfWidth*scale - 20;
        [self setLabelContentText:self.detailModel.couponsRemindAppoint Label:otherContentLable];
        [bgView addSubview:otherContentLable];
    }
    
    //线
    UIView *lineThree = [self creatLine];
    [bgView addSubview:lineThree];
    
    UIView *lastView = lineTwo == nil ? lineOne : lineTwo;
    [self addMasonryWithLastView:lastView titleLabel:timeTitleLabel beginAndEndNameLabel:everyDayBeginAndEndTimeNameLabel beginAndEndTimeLabel:everyDayBeginAndEndTimeLabel exceptNameLabel:exceptTimeNameLabel exceptTimeLabel:exceptTimeLabel validNameLabel:everyDayValidNameLabel validTimeLabel:everyDayValidLabel otherContentLabel:otherContentLable line:lineThree];
    
    //************ 餐厅名称
    UILabel *resNameTitlelabel = [self creatLabelWithTextColor:UIColorFromRGB(0x666666, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:1];
    resNameTitlelabel.text = LZLocalizedString(@"group_publisher", nil);
    [bgView addSubview:resNameTitlelabel];
    
    UILabel *resNamelabel = [self creatLabelWithTextColor:UIColorFromRGB(0x333333, 1) Font:[UIFont fontWithName:@"ArialMT" size:15*textScale] NumberLines:0];
    resNamelabel.preferredMaxLayoutWidth = selfWidth*scale - 20;
    [self setLabelContentText:self.detailModel.restaurantName Label:resNamelabel];
    [bgView addSubview:resNamelabel];
    
    UIView *lineFour = [self creatLine];
    [bgView addSubview:lineFour];
    
    [self addMasonryWithLastView:lineThree resNameTitleLabel:resNameTitlelabel resNameLabel:resNamelabel line:lineFour];
    
    //*****更新底图高度
    [bgView layoutIfNeeded];
    
    CGFloat maxY= lineFour.frame.origin.y;
    if (maxY < 348*scale) {
        maxY = 350.0;
    }
    [bgView updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(maxY);
    }];
    
    UIView *whiteBgView = [[UIView alloc] init];
    whiteBgView.backgroundColor = UIColorFromRGB(0xffffff, 1);
    [bgView insertSubview:whiteBgView atIndex:0];
    [whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(300);
    }];
    
//    //高斯模糊
//    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:effect];
//    [bgView insertSubview:visualView atIndex:0];
//    [visualView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.top.trailing.mas_equalTo(0);
//        make.bottom.mas_equalTo(300);
//    }];
    
  
}

#pragma mark - -----------------   masonry   ------------------
#pragma mark  优惠券名称, 数量. 抢券按钮, 线 约束
- (void)addMasonryWithCouponNamelLabel:(UILabel *)couponNameLabel couponCountLabel:(UILabel *)couponCountLabel line:(UIView *)line getCouponBtn:(UIButton *)getCouponBtn
{
    [couponNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.mas_equalTo(17);
        make.trailing.mas_equalTo(-123);
    }];
    
    [couponCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(couponNameLabel);
        make.top.equalTo(couponNameLabel.mas_bottom).offset(7);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(couponCountLabel.mas_bottom).offset(15);
    }];
    
    [getCouponBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(103, 36));
        make.top.mas_equalTo(18);
        make.trailing.mas_equalTo(-10);
    }];
}


#pragma mark - 添加 使用条件, 规则提醒 线  的约束
- (void)addMasonryWithLastView:(UIView *)lastView rulesTitleLabel:(UILabel *)titleLabel conditionsLabel:(UILabel *)conditionsLabel rluesLabel:(UILabel *)rulesLabel line:(UIView *)line
{
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.equalTo(lastView.mas_bottom).offset(15);
    }];
    
    UILabel *lastLabel = titleLabel;
    
    if (conditionsLabel) {
        lastLabel = conditionsLabel;
        [conditionsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.top.equalTo(titleLabel.mas_bottom).offset(13);
        }];
    }
    
    if (rulesLabel) {
        [rulesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.top.equalTo(lastLabel.mas_bottom).offset(13);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(rulesLabel.mas_bottom).offset(15);
        }];
    }else{
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(lastLabel.mas_bottom).offset(15);
        }];
    }
}

#pragma mark - 添加 每天开始结束时间, 除外时间, 每天有效时间, 预约提醒 label 约束
- (void)addMasonryWithLastView:(UIView *)lastView titleLabel:(UILabel *)titleLabel beginAndEndNameLabel:(UILabel *)beginAndEdnNameLabel beginAndEndTimeLabel:(UILabel *)beginAndEndTimeLabel exceptNameLabel:(UILabel *)exceptNameLabel exceptTimeLabel:(UILabel *)exceptTimeLabel validNameLabel:(UILabel *)validNameLabel validTimeLabel:(UILabel *)validTimeLabel otherContentLabel:(UILabel *)otherContentLabel line:(UIView *)line
{
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.equalTo(lastView.mas_bottom).offset(15);
    }];
    
    [beginAndEdnNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.top.equalTo(titleLabel.mas_bottom).offset(13);
    }];
    
    [beginAndEndTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(beginAndEdnNameLabel);
        make.trailing.lessThanOrEqualTo(-10);
        make.top.equalTo(beginAndEdnNameLabel.mas_bottom).offset(13);
    }];
    
    [exceptNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(beginAndEdnNameLabel);
        make.top.equalTo(beginAndEndTimeLabel.mas_bottom).offset(13);
    }];
    
    [exceptTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(exceptNameLabel.mas_trailing).offset(10);
        make.trailing.lessThanOrEqualTo(-10);
        make.top.equalTo(exceptNameLabel);
    }];
    
    [validNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(beginAndEdnNameLabel);
        make.top.equalTo(exceptTimeLabel.mas_bottom).offset(13);
    }];
    
    [validTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(validNameLabel.mas_trailing).offset(10);
        make.trailing.lessThanOrEqualTo(-10);
        make.top.equalTo(validNameLabel);
    }];
    
    if (otherContentLabel != nil) {
        [otherContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.top.equalTo(validTimeLabel.mas_bottom).offset(13);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(otherContentLabel.mas_bottom).offset(15);
        }];
    }else{
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.height.mas_equalTo(0.5);
            make.top.equalTo(validTimeLabel.mas_bottom).offset(15);
        }];
    }
   
    
    
}

#pragma mark - 添加  餐厅名称  label 约束
- (void)addMasonryWithLastView:(UIView *)lastView resNameTitleLabel:(UILabel *)titleLabel resNameLabel:(UILabel *)resNameLabel line:(UIView *)line
{
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.leading.mas_equalTo(10);
       make.top.equalTo(lastView.mas_bottom).offset(15);
    }];
    
    [resNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(10);
        make.top.equalTo(titleLabel.mas_bottom).offset(13);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.top.equalTo(resNameLabel.mas_bottom).offset(15);
        make.height.mas_equalTo(0.5);
    }];
}

#pragma mark - creat control
- (UILabel *)creatLabelWithTextColor:(UIColor *)textColor Font:(UIFont *)textFont NumberLines:(NSInteger)numLines
{
    UILabel *l = [[UILabel alloc] init];
    l.textColor = textColor;
    l.font = textFont;
    l.numberOfLines = numLines;
    l.backgroundColor = [UIColor clearColor];
    l.lineBreakMode = NSLineBreakByTruncatingTail;
    l.textAlignment = NSTextAlignmentNatural;
    
    return l;
}

- (UIView *)creatLine
{
    UIView * v = [[UIView alloc] init];
    v.backgroundColor = UIColorFromRGB(0xdddddd, 1);
    return v;
}

- (UIButton *)creatButtonWithTitle:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = UIColorFromRGB(0xff7a21, 1);
    btn.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:12];
    [btn setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.layer.cornerRadius = 4;
    btn.layer.masksToBounds = YES;
    
    return btn;
}
@end
